package cn.stylefeng.guns.modular.order.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.order.entity.OrderClientVolSum;
import cn.stylefeng.guns.modular.order.mapper.OrderClientVolSumMapper;
import cn.stylefeng.guns.modular.order.model.params.OrderClientVolSumParam;
import cn.stylefeng.guns.modular.order.model.result.OrderClientVolSumResult;
import  cn.stylefeng.guns.modular.order.service.OrderClientVolSumService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 待排车门店信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-25
 */
@Service
public class OrderClientVolSumServiceImpl extends ServiceImpl<OrderClientVolSumMapper, OrderClientVolSum> implements OrderClientVolSumService {

    @Override
    public void add(OrderClientVolSumParam param){
        OrderClientVolSum entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(OrderClientVolSumParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void deleteByDateDt(OrderClientVolSumParam param) {
        this.baseMapper.deleteByDateDt(param);
    }

    @Override
    public void update(OrderClientVolSumParam param){
        OrderClientVolSum oldEntity = getOldEntity(param);
        OrderClientVolSum newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ctj(OrderClientVolSumParam param) {
        OrderClientVolSum obj = this.getById(param.getId());
        OrderClientVolSumParam updateObj  = new OrderClientVolSumParam();
        updateObj.setUndisVolume(obj.getUndisVolume().subtract(param.getCVolume()));
        updateObj.setId(param.getId());
        OrderClientVolSum oldEntity = getOldEntity(param);
        OrderClientVolSum newEntity = getEntity(updateObj);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
        obj.setId(null);
        obj.setUndisVolume(param.getCVolume());
        this.save(obj);

    }

    @Override
    public int insertBatch(List<OrderClientVolSumParam> param) {
        return this.baseMapper.saveBatchList(param);
    }

    @Override
    public OrderClientVolSumResult findBySpec(OrderClientVolSumParam param){
        return null;
    }

    @Override
    public List<OrderClientVolSumResult> findListBySpec(OrderClientVolSumParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(OrderClientVolSumParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,null);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<OrderClientVolSumResult> getClientSumList(OrderClientVolSumParam paramCondition) {
        return this.baseMapper.getClientSumList(paramCondition);
    }

    @Override
    public List<OrderClientVolSumResult> getShuaiTuoList(OrderClientVolSumParam paramCondition, Long operateId) {
        return this.baseMapper.getShuaiTuoList(paramCondition,operateId);
    }

    private Serializable getKey(OrderClientVolSumParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private OrderClientVolSum getOldEntity(OrderClientVolSumParam param) {
        return this.getById(getKey(param));
    }

    private OrderClientVolSum getEntity(OrderClientVolSumParam param) {
        OrderClientVolSum entity = new OrderClientVolSum();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
